//
//  HomeCentreBannerView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/20.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit
import FSPagerView
import SDWebImage

protocol HomeCentreBannerViewDelegate:NSObjectProtocol {
    func HomeCentreBannerViewClick(url:String)
}

class HomeCentreBannerView: UIView {
    weak var delegate:HomeCentreBannerViewDelegate?
    
    var banner: [CentreBannerImagesModel]?
    
    // MARK: - 懒加载滚动图片浏览器
    private lazy var pagerView : FSPagerView = {
        let pagerView = FSPagerView()
        pagerView.delegate = self
        pagerView.dataSource = self
        pagerView.automaticSlidingInterval =  5
        pagerView.isInfinite = true
        pagerView.interitemSpacing = 10
        pagerView.transformer = FSPagerViewTransformer(type: .crossFading)
        pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "HomeBannerCell")
        pagerView.layer.cornerRadius = 10
        pagerView.layer.masksToBounds = true
        return pagerView
    }()
    
    var bannerList: [CentreBannerImagesModel]? {
        didSet {
            guard let model = bannerList else { return }
            self.banner = model
            self.pagerView.reloadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(self.pagerView)
        self.pagerView.frame = frame
        self.pagerView.itemSize = CGSize.init(width: frame.width, height: frame.height)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension HomeCentreBannerView: FSPagerViewDelegate, FSPagerViewDataSource {
    // MARK:- FSPagerView Delegate
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return self.banner?.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "HomeBannerCell", at: index)
        //        cell.imageView?.sd_setImage(with: URL(string:(self.banner?[index].imageUrl)!), completed: nil)
        cell.imageView?.backgroundColor = UIColor.green
        
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        let url:String = self.banner?[index].imageUrl ?? ""
        delegate?.HomeCentreBannerViewClick(url: url)
    }
}
